﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Wicture.QPZS.EPC.Services.LiYang
{
    public class WebClient
    {
        public string CrawHtml(string uri, string method, byte[] data, Dictionary<string, string> dicHeader, string contentType = "")
        {
            method = method.Trim().ToUpper();

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Timeout = 1000000;
            string html = string.Empty;
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Method = method;
            request.CookieContainer = new CookieContainer();
            if (!string.IsNullOrEmpty(contentType))
            {
                request.ContentType = contentType;
            }

            //request.Accept = "application/json, text/plain, */*";
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";
            if (dicHeader != null && dicHeader.Any())
            {
                foreach (var key in dicHeader.Keys)
                {
                    request.Headers.Add(key, dicHeader[key]);
                }
            }
            if (method == "POST")
            {

                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(data, 0, data.Length);
                }
            }

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                if (!string.IsNullOrEmpty(response.ContentEncoding))
                {
                    if (response.ContentEncoding.ToLower().Contains("gzip"))
                    {
                        using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
                        {
                            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                            {
                                html = reader.ReadToEnd();
                            }
                        }
                    }
                    else if (response.ContentEncoding.ToLower().Contains("deflate"))
                    {
                        using (DeflateStream stream = new DeflateStream(response.GetResponseStream(), CompressionMode.Decompress))
                        {
                            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                            {
                                html = reader.ReadToEnd();
                            }
                        }
                    }
                    else
                    {
                        using (Stream stream = response.GetResponseStream())
                        {
                            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                            {
                                html = reader.ReadToEnd();
                            }
                        }
                    }
                }
                else
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            html = reader.ReadToEnd();
                        }
                    }
                }
            }
            return html;
        }

    }
}
